package com.ctg.payroll2.action.sysadmin;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.util.Parse;
import com.opensymphony.xwork2.ActionSupport;

public class ChangePwdAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8383094803280297119L;
	private SpringSecurityManager springSecurityManager;
	String resultString;

	public String changPwd() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String userName = Parse.toString(request.getParameter("user_name"),
				false);
		String oldPassword = Parse.toString(request.getParameter("pwd"), false);
		String newPassword = Parse.toString(request.getParameter("newpwd"),
				false);

		try {
			SysUser user = (SysUser) ServletActionContext.getRequest()
					.getSession().getAttribute(Constants.CURRENT_USER);
			springSecurityManager.changePasswordByUsername(userName,
					oldPassword, newPassword, user);
			resultString = "";

		} catch (org.springframework.security.userdetails.UsernameNotFoundException e) {
			resultString = "只能修改启用状态帐号的密码或密码错误";
			setResultString(resultString);
		}

		return SUCCESS;
	}

	public void setSpringSecurityManager(
			SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}

	public String getResultString() {
		return resultString;
	}

	public void setResultString(String resultString) {
		this.resultString = resultString;
	}

}
